package com.cn.ccp.action.faculty;

import java.util.List;
import java.util.Map;

import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;

import org.apache.log4j.Logger;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.jsf.FacesContextUtils;

import com.cn.ccp.action.BaseAction;
import com.cn.ccp.bean.GradeBean;
import com.cn.ccp.bean.SpecialtyBean;
import com.cn.ccp.service.faculty.GradeService;
import com.cn.ccp.util.MsgConstants;

/**
 * 年级管理action
 * @author renyongcheng
 *
 */
public class GradeAction extends BaseAction{
	private Logger log = Logger.getLogger(GradeAction.class);
	private GradeService gradeService;
    private GradeBean gradeBean;
	private List<GradeBean> gradeList;
    private String gradeTitle,gradeName,gradeDescription;
    private SelectItem[] specialtyItem ;//专业下拉框值
    
    public GradeAction(){
    	super();
    	gradeTitle = getMessages(MsgConstants.GRADE_PAGE_TITLE);
    	gradeName = getMessages(MsgConstants.GRADE_PAGE_LABEL_GRADE_NAME);
    	gradeDescription = getMessages(MsgConstants.GRADE_PAGE_LABEL_GRADE_DES);
    	WebApplicationContext fcontextutils = FacesContextUtils
				.getWebApplicationContext(FacesContext.getCurrentInstance());
    	gradeService = (GradeService) fcontextutils
				.getBean("gradeService");
    	getpagedetail();
    }
    
	
	
	
	public void getpagedetail() {
		setItemCount(gradeService.getCountGrade());
		int currentpage = getCurrentPage();
		int pagesize = getPageSize();
		int start = (currentpage - 1) * pagesize;
		gradeList = gradeService.pageQueryGrade(start, pagesize);
	}
	
	public String edit(){
		if(gradeBean.getSpecialtyId().equals("-1")){
			showMsg("specialtyId", "javax.faces.validator.RequiredValidator.FAILED");
			return "";
		}
		if(gradeBean.getGradeType().equals("")){
			showMsg("specialtyname", "javax.faces.validator.RequiredValidator.FAILED");
			return "";
		}
		log.info("---------------------------");
		boolean flag = gradeService.gradeNameIsAlready(gradeBean.getId(), gradeBean.getGradeType());
		if(flag){
			showMsg("specialtyname", "grade_page_label_grade_name_failed");
			return "";
		}
		if(gradeBean.getId() == 0)
			return save();
		return update();
	}
	public String save(){
		boolean flag = gradeService.saveGrade(gradeBean);
		if(flag){
			getpagedetail();
			return MAIN;
		}
		return "";
	}
	
	public String update(){
		boolean flag = gradeService.updateGrade(gradeBean);
		if(flag){
			getpagedetail();
			return MAIN;
		}
		return "";
	}

	public String goUpdate(){
		Map<String, String> varMap = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
		String id = varMap.get("id");
		log.info("---------------------------------"+id);
		if(id != null)
			gradeBean =  (GradeBean)gradeService.findGradeById(Integer.parseInt(id));
		return UPDATE;
	}
	
	
	
	public void doDelete(){
		FacesContext context = FacesContext.getCurrentInstance();// 取得上下文
		String id = (String)context.getExternalContext().getRequestParameterMap().get("id");// 得到页面的参数
		gradeService.deleteGrade(Integer.parseInt(id));
		getpagedetail();
		log.info("-----------------doDelete()----------------"+id);
	}




	public GradeService getGradeService() {
		return gradeService;
	}




	public void setGradeService(GradeService gradeService) {
		this.gradeService = gradeService;
	}



	public GradeBean getGradeBean() {
		if(gradeBean == null)
			gradeBean = new GradeBean();
		return gradeBean;
	}




	public void setGradeBean(GradeBean gradeBean) {
		this.gradeBean = gradeBean;
	}




	public List<GradeBean> getGradeList() {
		return gradeList;
	}




	public void setGradeList(List<GradeBean> gradeList) {
		this.gradeList = gradeList;
	}




	public String getGradeTitle() {
		return gradeTitle;
	}




	public void setGradeTitle(String gradeTitle) {
		this.gradeTitle = gradeTitle;
	}




	public String getGradeName() {
		return gradeName;
	}




	public void setGradeName(String gradeName) {
		this.gradeName = gradeName;
	}




	public String getGradeDescription() {
		return gradeDescription;
	}




	public void setGradeDescription(String gradeDescription) {
		this.gradeDescription = gradeDescription;
	}




	public SelectItem[] getSpecialtyItem() {
		List<SpecialtyBean> specialtyList = getSpecialtyService().pageQuerySpecialty(0, 20);
		if(specialtyList == null){
			specialtyItem = new SelectItem[0];
		}else{
			specialtyItem = new SelectItem[specialtyList.size()];
			for(int i = 0 ; i < specialtyList.size() ; i++){
				SpecialtyBean bean = specialtyList.get(i);
				specialtyItem[i] = new SelectItem(String.valueOf(bean.getId()) , bean.getSpecialtyname());
			}
		}
		return specialtyItem;
	}




	public void setSpecialtyItem(SelectItem[] specialtyItem) {
		this.specialtyItem = specialtyItem;
	}

	
	
}
